package com.ymtms.arrivalofgoods.service.impl;

import com.ymtms.arrivalofgoods.dao.ArriveErrorMapper;
import com.ymtms.arrivalofgoods.dao.GoodsbillMasterMapper;
import com.ymtms.arrivalofgoods.dao.GoodsbillReceoptMainMapper;
import com.ymtms.arrivalofgoods.exception.FileUploadException;
import com.ymtms.arrivalofgoods.pojo.ArriveError;
import com.ymtms.arrivalofgoods.pojo.GoodsbillMaster;
import com.ymtms.arrivalofgoods.pojo.GoodsbillReceoptMain;
import com.ymtms.arrivalofgoods.service.ArriveErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ArriveErrorServiceImpl implements ArriveErrorService {
    @Autowired
    private ArriveErrorMapper arriveErrorMapper;
    @Autowired
    private GoodsbillReceoptMainMapper goodsbillReceoptMainMapper;
    @Autowired
    private GoodsbillMasterMapper goodsbillMasterMapper;


    @Override
    public void saveArriveService(Integer num,ArriveError arriveError, MultipartFile file,String filepath) {
        System.out.println(arriveError);
        System.out.println(file);
        if (!file.isEmpty()) {
            File upload=new File(filepath);
            if(!upload.exists()){
               upload.mkdirs();
            }
            String filename = file.getOriginalFilename();
            String sdf = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String newFileName=sdf+filename;
            //文件归档
            try {
                file.transferTo(new File(filepath+File.separator+newFileName));
                GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.selectByPrimaryKey(num);
                GoodsbillMaster goodsbillMaster = goodsbillMasterMapper.selectByPrimaryKey(goodsbillReceoptMain.getGoodsBillpkid());
                arriveError.setCustomer(goodsbillMaster.getSendGoodsCustomer());
                arriveError.setGoodsRevertBillcode(goodsbillReceoptMain.getGoodsRevertBillCode());
                arriveError.setPackagePath(filepath+File.separator+newFileName);
                arriveErrorMapper.insertSelective(arriveError);
                goodsbillReceoptMainMapper.updateExecuteByGrmid(num);
            } catch (IOException e) {
                e.printStackTrace();
                throw new FileUploadException("文件上传失败");
            }


        }


    }

    @Override
    public void saveArriveService(ArriveError arriveError, MultipartFile file, String filepath) {
        if (!file.isEmpty()) {
            File upload=new File(filepath);
            if(!upload.exists()){
                upload.mkdirs();
            }
            String filename = file.getOriginalFilename();
            String sdf = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String newFileName=sdf+filename;
            //文件归档
            try {
                String goodsRevertBillcode=null;
                file.transferTo(new File(filepath+File.separator+newFileName));
                if (arriveError!=null&&arriveError.getGoodsRevertBillcode()!=null){
                 goodsRevertBillcode = arriveError.getGoodsRevertBillcode();
                }
                GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.queryReceiptByBillCode(goodsRevertBillcode);
                GoodsbillMaster goodsbillMaster = goodsbillMasterMapper.selectByPrimaryKey(goodsbillReceoptMain.getGoodsBillpkid());
                arriveError.setCustomer(goodsbillMaster.getSendGoodsCustomer());
                arriveError.setGoodsRevertBillcode(goodsbillReceoptMain.getGoodsRevertBillCode());
                arriveError.setPackagePath(filepath+File.separator+newFileName);
                arriveErrorMapper.insertSelective(arriveError);
                goodsbillReceoptMainMapper.updateExecuteByGrmid(goodsbillReceoptMain.getGrmid());
            } catch (IOException e) {
                e.printStackTrace();
                throw new FileUploadException("文件上传失败");
            }


        }

    }

    @Override
    public ArriveError findArriveErrorByAid(Integer aid) {
        return arriveErrorMapper.selectByPrimaryKey(aid);
    }

    @Override
    public List<ArriveError> queryAllArriveError(Integer currentPage, Integer pageSize,ArriveError arriveError) {



        return  arriveErrorMapper.findAllError(currentPage,pageSize,arriveError);
    }
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    @Override
    public void deleteReceiptByNum(Integer num) {

        ArriveError arriveError = arriveErrorMapper.selectByPrimaryKey(num);
        goodsbillReceoptMainMapper.updateExecuteToZero(arriveError.getGoodsRevertBillcode());
        arriveErrorMapper.deleteByPrimaryKey(num);
    }

    @Override
    public void modifyArriveError(ArriveError arriveError) {
        arriveError.setPackagePath(arriveErrorMapper.selectByPrimaryKey(arriveError.getAid()).getPackagePath());

        arriveErrorMapper.updateByPrimaryKeySelective(arriveError);
    }
}
